package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.User;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {
    @Reference
    CheckGroupService checkGroupService;
    //添加检查组
    @RequestMapping("/addGroup")
    public Result addGroup(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        try{
            checkGroupService.addGroup(checkGroup, checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){

            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }
    //查询所有检查组（分页，条件查询）
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult = checkGroupService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
        }catch (Exception e){

            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //根据id查询检查组
    @RequestMapping("/findCheckGroupById")
    public Result findCheckGroupById(Integer checkGroupId){
        try{
            CheckGroup checkGroup = checkGroupService.findCheckGroupById(checkGroupId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        }catch (Exception e){

            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    //根据id查询检查项
    @RequestMapping("/findCheckItemById")
    public Result findCheckItemById(Integer checkGroupId){
        try{
            List<Integer> checkitemIds = checkGroupService.findCheckItemById(checkGroupId);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //修改后保存
    @RequestMapping("/editGroupAndSave")
    public Result editGroupAndSave(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        try{

            checkGroupService.editGroupAndSave(checkGroup,checkitemIds);

            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    //查询所有检测组
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<CheckGroup> checkGroupList = checkGroupService.findAll();
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS,checkGroupList);
        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }
}
